//
//  YMMainPlayerVC.m
//  SmartCar
//
//  Created by y s on 2022/12/25.
//

#import "YMMainPlayerVC.h"
#import "YMPlayerFinishedView.h"
#import "UINavigationController+FDFullscreenPopGesture.h"

@interface YMMainPlayerVC ()<YMPlayerViewDelegate>

@property (nonatomic, strong) UIButton *closeBtn;
@property (nonatomic, strong) UIView *navView;
@property (nonatomic, strong) UIView *backView;

@property (nonatomic, strong) YMPlayerFinishedView *vFinished;


@end

@implementation YMMainPlayerVC

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setUI];
    [self layout];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];

    self.navBarHidden    = YES;
    self.statusBarHidden = YES;
    self.fd_interactivePopDisabled = YES;
    
    if(_playEnded == NO && _isPlaying){
       if(_vPlayer.urlString.length){
           [_vPlayer play];
       } else{
           [_vPlayer playWithUrlString:_url];
       }
    }
    if(_isShowFull){
        [self rotatePlayerView];
    }
}

- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    self.fd_interactivePopDisabled = NO;
    self.navBarHidden = NO;
    self.statusBarHidden = NO;
}

- (void)setUI{
    self.navBarHidden = YES;
    self.view.backgroundColor = [UIColor blackColor];
    
    _vFinished = [[YMPlayerFinishedView alloc] init];
    _vFinished.hidden = YES;
    if (self.vPlayer == nil){
        self.vPlayer = [[YMMainPlayerView alloc] init];
    }

    [self.view addSubview:self.backView];
    [self.backView addSubview:self.vPlayer];
    [self.backView addSubview:self.navView];
    
    [_vPlayer showToolBar];
    [self.navView addSubview:_vPlayer.topTitle];
    
    [_vPlayer addSubview:self.vFinished];
    
    __weak typeof(self) weakSelf = self;
    self.vFinished.replayBlock = ^{
        weakSelf.vPlayer.notCanTouch = NO;
        [weakSelf.vPlayer play];
    };
    _vPlayer.delegate = self;
    _vPlayer.toolView.hidden   = NO;
    _vPlayer.playButton.hidden = NO;
}


- (void)layout {
    _vPlayer.frame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT-IPHONE_X_BOTTOM_HEIGHT);
    _navView.frame = CGRectMake(0, 0, SCREEN_WIDTH, TOP_BAR_HEIGHT);
    [_vPlayer mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.centerY.centerX.equalTo(_backView);
        make.size.mas_equalTo(CGSizeMake(SCREEN_WIDTH,SCREEN_HEIGHT-IPHONE_X_BOTTOM_HEIGHT));
    }];
    [_vPlayer.topTitle mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_closeBtn.mas_right);
        make.centerY.equalTo(_closeBtn);
    }];
    [_vFinished mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(UIEdgeInsetsMake(0, 0, 0, 0));
    }];
}



#pragma mark -- Event action
- (void)rotatePlayerView{
    __weak typeof(self) weakSelf = self;
    [UIView animateWithDuration:0.25 animations:^{
        weakSelf.backView.width  = SCREEN_HEIGHT;
        weakSelf.backView.height = SCREEN_WIDTH;
        weakSelf.backView.center = CGPointMake(SCREEN_WIDTH/2, SCREEN_HEIGHT/2);
        // 旋转
        weakSelf.backView.transform = CGAffineTransformMakeRotation(M_PI_2);
        [weakSelf.backView layoutIfNeeded];
        [weakSelf.closeBtn mas_updateConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(STATUS_BAR_HEIGHT+17);
            make.top.mas_equalTo(17.5);
        }];
        weakSelf.vPlayer.frame = CGRectMake(0, 0, SCREEN_HEIGHT, SCREEN_WIDTH);
        weakSelf.vPlayer.fullScreen = YES;
        [weakSelf.vPlayer mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.edges.mas_equalTo(0);
        }];
        [weakSelf.vPlayer.toolView mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(STATUS_BAR_HEIGHT);
            make.right.mas_equalTo(-STATUS_BAR_HEIGHT);
            make.bottom.mas_equalTo(0);
            make.height.mas_equalTo([SFitUtil fit:40]);
        }];
        [weakSelf.vPlayer.vRateBg mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.right.mas_equalTo([SFitUtil fit:-50]-STATUS_BAR_HEIGHT);
            make.bottom.equalTo(weakSelf.vPlayer.toolView.mas_top);
            make.size.mas_equalTo([SPlayerMultipleSheet getViewSize:YES]);
        }];
        [weakSelf.vPlayer setNeedsLayout];

        weakSelf.navView.frame = CGRectMake(0, 0, SCREEN_HEIGHT, TOP_BAR_HEIGHT);
        [weakSelf.vPlayer.topTitle mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(weakSelf.closeBtn.mas_right);
            make.centerY.equalTo(weakSelf.closeBtn);
        }];
        
    } completion:^(BOOL finished) {
        weakSelf.vPlayer.fullScreen = YES;
    }];
}
- (void)restoreBackView {
    _vPlayer.fullScreen = NO;
    self.backView.transform = CGAffineTransformIdentity;
    
    self.backView.frame  = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
    self.vPlayer.frame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT-IPHONE_X_BOTTOM_HEIGHT);
    [_vPlayer mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.centerY.centerX.equalTo(_backView);
        make.size.mas_equalTo(CGSizeMake(SCREEN_WIDTH,SCREEN_HEIGHT-IPHONE_X_BOTTOM_HEIGHT));
    }];
    self.navView.frame = CGRectMake(0, 0, SCREEN_WIDTH, TOP_BAR_HEIGHT);
    [self.closeBtn mas_updateConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo([SFitUtil fit:15]);
        make.top.mas_equalTo(STATUS_BAR_HEIGHT+13);
    }];
    [_vPlayer.toolView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.mas_equalTo(0);
        make.height.mas_equalTo([SFitUtil fit:40]);
    }];
}


- (void)close{
    if (_isShowFull){
        [self restoreBackView];
        [self.vPlayer resetAll:YES];
        if(self.backAction){
            self.backAction();
        }
        [self.navigationController popViewControllerAnimated:NO];
    }else{
        if (_vPlayer.fullScreen){
            [self restoreBackView];
        }else{
            [self.vPlayer resetAll:YES];
            if(self.backAction){
                self.backAction();
            }
            [self.navigationController popViewControllerAnimated:NO];
        }
    }
}

#pragma mark -- PlayerViewDelegate
- (void)clickPlayerFullScreen:(YMMainPlayerView *)playerView{
    if (_vPlayer.fullScreen == YES) {
        [self rotatePlayerView];
    }else{
        if (_isShowFull){
            [self close];
        }else{
            [self restoreBackView];
        }
    }
}
- (void)toolBarChangeHidden{

}

- (void)playFinished{
    _vFinished.hidden  = NO;
    _vPlayer.notCanTouch = YES;
}

#pragma mark -- setter and getter
- (UIButton *)closeBtn{
    if(!_closeBtn){
        _closeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_closeBtn setImage:[UIImage imageNamed:@"tools_nav_white_back"] forState:UIControlStateNormal];
        [_closeBtn g_clickEdgeWithTop:20 bottom:20 left:20 right:20];
        [_closeBtn addTarget:self action:@selector(close) forControlEvents:UIControlEventTouchUpInside];
    }
    return _closeBtn;
}
- (UIView *)navView{
    if (_navView == nil) {
        _navView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, TOP_BAR_HEIGHT)];
        [_navView addSubview:self.closeBtn];
        
        [_closeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo([SFitUtil fit:15]);
            make.top.mas_equalTo(STATUS_BAR_HEIGHT+13);
            make.size.mas_equalTo(CGSizeMake(18, 18));
        }];
    }
    return _navView;
}
- (UIView *)backView{
    if(!_backView){
        _backView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
        _backView.backgroundColor = [UIColor clearColor];
    }
    return _backView;
}


@end
